package com.nf.health.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.customview.SwipeView.OnSwipeStatusChangeListener;
import com.nf.health.app.models.TempertuerHistoryModel;

import java.util.ArrayList;
import java.util.List;

public class TemperatureHistoryDataAdapter extends BaseAdapter {

	private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

	private Context context;
	private List<TempertuerHistoryModel> list;



	public TemperatureHistoryDataAdapter(Context context, List<TempertuerHistoryModel> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public View getView(final int position, View view, ViewGroup viewGroup) {
		final ViewHolder holder;
		if (view == null) {
			view = View.inflate(context, R.layout.item_tempertate_history, null);
			holder = new ViewHolder();
			holder.tv_temperature_history = (TextView) view.findViewById(R.id.tv_temperature_history);
			holder.tv_temperature_date_history = (TextView) view.findViewById(R.id.tv_temperature_date_history);
			holder.swipeView  = (SwipeView) view.findViewById(R.id.swipeView);
			holder.delete  = view.findViewById(R.id.delete);
			unClosedSwipeView.add(holder.swipeView);
			view.setTag(holder);
			
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final TempertuerHistoryModel model = list.get(position);
		holder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(position, "");
				unClosedSwipeView.clear();
			}
		});
		if(model.getIsOpen()){
			holder.swipeView.setSwipeStatus(SwipeView.SwipeStatus.Open);
		}else{
			holder.swipeView.setSwipeStatus(SwipeView.SwipeStatus.Close);
		}
		holder.swipeView.setOnSwipeStatusChangeListener(new OnSwipeStatusChangeListener() {

			@Override
			public void onSwiping(SwipeView swipingSwipeView) {
				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					closeAllOpenedSwipeView();
				}

				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					unClosedSwipeView.add(swipingSwipeView);
				}

			}

			@Override
			public void onOpen(SwipeView openedSwipeView) {
				for (int i = 0; i < unClosedSwipeView.size(); i++) {
					if (unClosedSwipeView.get(i) != openedSwipeView) {
						unClosedSwipeView.get(i).close();
					}
				}
				if (!unClosedSwipeView.contains(openedSwipeView)) {
					unClosedSwipeView.add(openedSwipeView);
				}
				for(int i = 0 ; i < list.size(); i++){
					if(i != position){
						model.setIsOpen(false);
					}else{
						model.setIsOpen(true);
					}
				}

			}

			@Override
			public void onClose(SwipeView closedSwipeView) {
				unClosedSwipeView.remove(closedSwipeView);
			}
		});
		
		String date = model.getPosttime();
		String temperature = model.getTemp();
		holder.tv_temperature_date_history.setText(TextUtils.isEmpty(date) ? "未知" :date);
		holder.tv_temperature_history.setText(TextUtils.isEmpty(temperature) ? "未知" :temperature);

		/*holder.history_rq.setText(Str[0]);
		holder.history_bs.setText(doctors.get(position).getSetnum());
		holder.history_jl.setText(doctors.get(position).getKilometer());
		double sleepDuration = Double.parseDouble(doctors.get(position).getSleep());
		holder.history_sm.setText(String.format("%.2f", sleepDuration/3600f));
		holder.history_kll.setText(doctors.get(position).getCalorie());

		holder.history_sm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(listener != null)
					listener.onClick(position, "sm");	
			}
		});*/
		
		return view;
	}

	class ViewHolder {
		TextView tv_temperature_date_history, tv_temperature_history;
		SwipeView swipeView;
		View delete;
	}
	
	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){

		this.listener = listener;
	}
	public void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
	public void setMyNotifyDatachanged(List<TempertuerHistoryModel> list){
			this.list = list;
		notifyDataSetChanged();
		closeAllOpenedSwipeView();
	}
}
